package com.example.service.impl;

import com.example.dao.AddressDao;
import com.example.dao.DistrictDao;
import com.example.dao.UserInfoDao;
import com.example.dto.AddressTDO;
import com.example.entity.Address;
import com.example.entity.District;
import com.example.service.AddressService;
import com.example.vo.AddressVo;
import com.example.vo.DistrictVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class AddressServiceImpl implements AddressService {

    @Autowired
    private AddressDao addressDao;

    @Autowired
    private DistrictDao districtDao;

    @Autowired
    private UserInfoDao userInfoDao;

    @Override
    public List<AddressVo> getAddressByUserId(Integer userId) {

        List<Address> addressList = addressDao.getAddressByUserId(userId);

        List<AddressVo> addressVoList = new ArrayList<>();
        addressList.forEach(address -> addressVoList.add(convert(address)));
        return addressVoList;
    }

    @Override
    public void addAddress(AddressTDO addressTDO) {

//        根据用户id获取用户名
        String loginUserName = userInfoDao.getName(addressTDO.getUserId());
//        转为address对象
        Address address = convert(addressTDO);

        address.setCreatedUser(loginUserName);
        address.setModifiedUser(loginUserName);

        addressDao.addAddress(address);
    }

    @Override
    public void deleteAddressById(Integer id) {

        addressDao.deleteAddressById(id);
    }

    @Override
    public void updateAddress(AddressTDO addressDTO) {

        String loginUserName = userInfoDao.getName(addressDTO.getUserId());

        Address address = convert(addressDTO);

        address.setId(addressDTO.getId());
        address.setModifiedUser(loginUserName);

        addressDao.updateAddress(address);
    }

    @Override
    public Address convert(AddressTDO addressTDO) {

        Address address = new Address();

        address.setUserId(addressTDO.getUserId());
        address.setLevel(addressTDO.getLevel());
        address.setName(addressTDO.getName());

        address.setZip(addressTDO.getZip());
        address.setPhone(addressTDO.getPhone());
        address.setTel(addressTDO.getTel());
        address.setTag(addressTDO.getTag());

        address.setIsDefault(addressTDO.getIsDefault());

        address.setProvinceName(addressTDO.getProvinceName());
        address.setCityName(addressTDO.getCityName());
        address.setAreaName(addressTDO.getAreaName());
        address.setProvinceCode(addressTDO.getProvinceCode());
        address.setCityCode(addressTDO.getCityCode());
        address.setAreaCode(addressTDO.getAreaCode());

        address.setAddress(addressTDO.getAddress());

        return address;
    }

    @Override
    public AddressVo convert(Address address) {
        AddressVo addressVo = new AddressVo();
        addressVo.setId(address.getId());
        addressVo.setUserId(address.getUserId());
        addressVo.setLevel(address.getLevel());
        addressVo.setName(address.getName());
        addressVo.setZip(address.getZip());
        addressVo.setPhone(address.getPhone());
        addressVo.setTel(address.getTel());
        addressVo.setTag(address.getTag());
        addressVo.setIsDefault(address.getIsDefault());
        addressVo.setProvinceName(address.getProvinceName());
        addressVo.setCityName(address.getCityName());
        addressVo.setAreaName(address.getAreaName());
        addressVo.setProvinceCode(address.getProvinceCode());
        addressVo.setCityCode(address.getCityCode());
        addressVo.setAreaCode(address.getAreaCode());
        addressVo.setAddress(address.getAddress());
        return addressVo;
    }

    @Override
    public AddressVo getAddressById(Integer id) {

        Address address = addressDao.getAddressById(id);

        return convert(address);

    }

    @Override
    public void setDefault(Integer id, Integer userId) {
        int i = this.addressDao.setNunDefault(userId);

        String userName = userInfoDao.getName(userId);

        // 取消原有默认地址
        addressDao.cancelDefault(userId);
        // 设置新的默认地址
        addressDao.setDefault(id, userName);
    }

    @Override
    public List<DistrictVo> getDistrictByParentId(Integer parent) {

        List<District> districtList = districtDao.getDistrictByParentId(parent);

        List<DistrictVo> districtVoList = new ArrayList<>();

        districtList.forEach(district -> {
            DistrictVo districtVo = new DistrictVo();
            districtVo.setName(district.getName());
            districtVo.setCode(district.getCode());

            districtVoList.add(districtVo);
        });

        return districtVoList;
    }

    @Override
    public List<String> getOrderAddress(Integer userId) {

        Address defaultAddress = addressDao.getDefaultAddress(userId);

        List<String> orderAddress = new ArrayList<>();

        orderAddress.add(defaultAddress.getName() + "   " + defaultAddress.getPhone());
        orderAddress.add(defaultAddress.getProvinceName() + defaultAddress.getCityName() +
                defaultAddress.getAreaName() + defaultAddress.getAddress());

        return orderAddress;
    }

    /**
     * 获取默认地址
     * @param userId
     */
    @Override
    public Address getDefaultAddress(Long userId) {
        Address defaultAddress = this.addressDao.getDefaultAddress(Math.toIntExact(userId));
        defaultAddress.packAddressDetail();
        return defaultAddress;
    }
}
